<template>
  <div class="area-page" v-loading="loading" element-loading-text="加载中...">
    <div class="area-title">
      <div class="area-title-left">
        <!-- <el-form inline>
          <el-form-item>
            <el-select v-model="searchForm.status" clearable placeholder="状态">
              <el-option label="有效" :value="1" />
              <el-option label="无效" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchFun">查询</el-button>
            <el-button @click="resetFun">重置</el-button>
          </el-form-item>
        </el-form> -->
        地区数据
      </div>
      <div class="area-title-right">
        <!-- <el-button
          type="danger"
          :disabled="selectData.length == 0 ? true : false"
          @click="delAllFun"
        >
          <template #icon>
            <IEpDelete />
          </template>
          批量删除</el-button
        > -->
        <el-button type="primary" :icon="Plus" @click="addDeptFun('add')">
          添加
        </el-button>
      </div>
    </div>
    <div class="area-table">
      <el-table
        border
        stripe
        :data="tableData"
        :height="store.tableHeight"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'sub', hasChildren: 'hasChildren' }"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column prop="name" label="区域名称" show-overflow-tooltip />
        <el-table-column prop="pinyin" label="区域拼音" />
        <el-table-column prop="code" label="区域邮编" />
        <el-table-column prop="first" label="首字母" />
        <!-- <el-table-column prop="status" label="状态" width="90">
          <template #default="{ row }">
            <el-switch
              disabled
              v-model="row.status"
              active-text="有效"
              :active-value="1"
              inactive-text="无效"
              :inactive-value="0"
              inline-prompt
              @change="statusFun(row)"
            />
          </template>
        </el-table-column> -->
        <el-table-column prop="lng" label="所在经度" />
        <el-table-column prop="lat" label="所在纬度" />
        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              @click="updateDeptFun('update', row)"
            >
              编辑
            </el-button>
            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <addDialog
    :treeData="tableData"
    ref="addDialogRef"
    @refreshList="refreshList"
  />
  <imgDialog ref="imgDialogRef" />
</template>
<script setup>
import { Plus } from "@element-plus/icons-vue";
import { useCounterStore } from "@/stores/counter.js";
import { listApi, delApi } from "@/plugins/api/shopping/area";
import addDialog from "./components/DialogArea.vue";
const store = useCounterStore();
let searchForm = ref({});
function searchFun() {
  selectData.value = [];
  tableData.value = [];
  getData();
}
function resetFun() {
  searchForm.value = {};
  tableData.value = [];
  selectData.value = [];
  getData();
}
let tableData = ref([]);
let loading = ref(false);
function getData() {
  loading.value = true;
  listApi(searchForm.value)
    .then((data) => {
      tableData.value = data;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
let addDialogRef = ref(null);
function addDeptFun(type) {
  addDialogRef.value.initFun(type);
}
function updateDeptFun(type, row) {
  addDialogRef.value.initFun(type, row);
}
function refreshList() {
  getData();
}
let selectData = ref([]);
function handleSelectionChange(val) {
  selectData.value = val;
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
function imgSrc(path) {
  return import.meta.env.VITE_APP_BASE_API + path;
}
function delAllFun() {
  ElMessageBox.confirm("确认批量删除选中的数据，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      let arr = selectData.value.map((ele) => {
        return ele.id;
      });
      delApi({
        id: arr.join(","),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        selectData.value = [];
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}

function statusFun(row) {
  stateApi({
    status: row.status,
    id: row.id,
  }).then(() => {
    ElMessage({
      type: "success",
      message: "修改成功！",
    });
    getData();
  });
}
// 查看图片
let imgDialogRef = ref(null);
function lookImg(src) {
  imgDialogRef.value.initFun(src);
}
onMounted(() => {
  getData();
});
</script>
<style scoped lang="scss">
.area-page {
  width: 100%;
  height: 100%;

  .area-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .area-title-left {
      align-self: center;
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;

      :deep(.el-form-item) {
        margin-bottom: 0;
      }
    }
  }

  .area-table {
    padding: 10px;
    box-sizing: border-box;
    .table-img {
      width: 50px;
      height: 50px;
      object-fit: cover;
      cursor: pointer;
      &:hover {
        transform: scale(0.97);
        transition: all 0.3s;
      }
    }
  }
}
</style>
